// detail.js
let app = getApp();
import util from '../../../utils/util.js';
var logincontent = '尚未登录，请先登录吗？';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showView:false,
    id: 0,
    info:'',
    pics:{},
    stars: [0, 1, 2, 3, 4],
    normalSrc: '../../../images/starnormal.png',
    selectedSrc: '../../../images/starselected.png',
    halfSrc: '../../../images/starhalf.png',
    key: 2,//评分
    key2:3,
    commentlist:'',
    piclist:'',
    favstatus:0,
    loadingHidden:true,
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    wx.setNavigationBarTitle({
      title: '详情'
    });

    var scene = decodeURIComponent(options.scene)

    // scene = '5b7e584d438f266bd411fb4a';
    this.setData({
      shopid: scene
    });
    this.getDetail();

    // let data = JSON.parse(options.data);
    // console.log(data);
    // let strary = data.pics.split(",");

    
    // let strarytmp=[];
    // for(var index  in strary)
    // {
    //   var str = strary[index];
    //     if(str !='')
    //     {
    //       var obj={};
    //       str = app.globalData.SERVER+'file/pic/'+str+'.png';
    //       obj.id = index;
    //       obj.url = str;
    //       strarytmp.push(obj);
    //     }
    // }

    // var showtype = false;
    // if (!data.shopservice && data.shopservice.length>0)
    // {
    //   showtype = true;
    // }
    // this.setData({
    //   info: data,
    //   showView:showtype,
    //   pics: strarytmp,
    //   key: data.serviceAttitudeScore,
    //   key2: data.serviceSpeedScore,
    // });

    this.getlist();
    this.isFav();
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  showdetail:function (obj) {

    var objkey = parseInt(obj.currentTarget.id);
    var ret = this.data.commentlist[objkey];
    var that = this;

    // var ret = this.data.info;
    let data = JSON.stringify(ret);
    wx.navigateTo({
      url: '../commentdetail/commentdetail?data=' + data,
    });
  },

  addschedule:function(){
    // var usetinfo = wx.getStorageSync("user_info");
    var usetinfo = util.getData("userinfo");
    if (usetinfo == '') 
    {
      var that = this;
      wx.showModal({
        title: '提醒',
        content: logincontent,
        success: function (eci) {
          if (eci.confirm) {
            wx.navigateTo({
              url: '../../auth/login/login'
            })
          } else {
          }
        }
      });
      // return;
    }else{

    
    var that = this;
    
    var ret = this.data.info;
    let data = JSON.stringify(ret);
    wx.navigateTo({
      url: '../../schedule/newschedule/newschedule?data=' + data,
    });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // 页面隐藏

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // 页面关闭
  },

  callphone:function(){
    var that = this;
     wx.makePhoneCall({
       phoneNumber: that.data.info.tel,
     
     })
  },
 
  showMap:function(){
    var that = this;
    if(this.data.info.location.length==2)
    {

      var lat = this.data.info.location[1];
      var lon = this.data.info.location[0];

      wx.openLocation({
        latitude: lat,
        longitude: lon,
        scale: 18,
        name: that.data.info.shopname,
        address: that.data.info.address,
      })
    }else{
      util.showToast('数据不全暂时无法定位');
    }

    

  },

  getlist() {
    var url = app.globalData.SERVER +'comment/getOneShopAllRepairComments';

    var showinfo = [];
    var shopid = this.data.info._id
    util.request(url, 'POST', {

      page: '0',
      pagesize: '100',
      shopid: shopid,
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        console.log(err.data.ret);

        let strarytmp = [];
        var list = err.data.ret;
        for (var i in list) {
          var listinfo = list[i];
          var pics = listinfo.pics;
        if (pics.length>0)
        {
          var piclist = pics.split(',');
          strarytmp=[];
        for (var info in piclist) {
          var str = piclist[info];
          if (str != '') {
            var obj = {};
            str = app.globalData.SERVER + 'file/pic/' + str + '.png';
            obj.id = info;
            obj.url = str;
            strarytmp.push(obj);
          }
        }
          listinfo.piclist = strarytmp;
          showinfo.push(listinfo);
        }else{
          showinfo.push(listinfo);
        }
        }
        this.setData({
          commentlist: showinfo,
          // piclist:strarytmp,
        });
        var num = 1;
      } else {
      
      }


    });
  },



onShareAppMessage: function (res) {
      if(res.from === 'button') {
  // 来自页面内转发按钮
  console.log(res.target)
}
return {
  title: this.data.info.shopname,
  path: '/pages/carstore/detail/detail',//实验发现，此处的路径应该写全，写到具体的wxml页面。如path:'/page/user/user?id=123'
  success: function (res) {
    // 转发成功
  },
  fail: function (res) {
    // 转发失败
  }
}

  },

setfav: function () {
  this.setData({
    loadingHidden: false
  });
  var usetinfo = util.getData("userinfo");
  if (usetinfo == '') {
    var that = this;
    wx.showModal({
      title: '提醒',
      content: logincontent,
      success: function (eci) {
        if (eci.confirm) {
          wx.navigateTo({
            url: '../../auth/login/login'
          })
        } else {
        }
      }
    });
 
  }else{
    var that = this;
  var url = app.globalData.SERVER + 'fav/add';

    if (this.data.favstatus ==1)
    {
      url = app.globalData.SERVER + 'fav/del';
    }
    var shopid = this.data.info._id
  util.request(url, 'POST', {
    carerid: usetinfo._id,
    shopid: shopid,
  }).then(res => {
    that.setData({
      loadingHidden: true,
    });
    util.showToast('成功', 'error', 2500);
  }).catch(err => {
    that.setData({
      loadingHidden: true,
    });
    if (err.data.code === 1) {
      var ret = 0;
      if (that.data.favstatus == 0)
      {
          ret = 1;
      }else{
          ret = 0;
      }
      that.setData({
        favstatus: ret,
        // piclist:strarytmp,
      });

    } else {

    }


  });
  }

  },

  isFav :function(){
    var usetinfo = util.getData("userinfo");
    var that = this;
    if (usetinfo == '') 
    {
      return;
    }
    var shopid = this.data.info._id
    var url = app.globalData.SERVER + 'fav/isFav';
    util.request(url, 'POST', {
      carerid: usetinfo._id,
      shopid: shopid,
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        
        that.setData({
          favstatus: err.data.ret,
          // piclist:strarytmp,
        });
       
      } else {

      }


    });

  },
  getDetail: function () {
    var usetinfo = util.getData("userinfo");
    var that = this;
    
    var shopid = this.data.shopid
    var url = app.globalData.SERVER + 'users/getShopInfo';
    util.request(url, 'POST', {
      id: shopid,
    }).then(res => {
      util.showToast('成功', 'error', 2500);
    }).catch(err => {
      if (err.data.code === 1) {
        var data = err.data.ret;
        let strary = data.pics.split(",");


        let strarytmp = [];
        for (var index in strary) {
          var str = strary[index];
          if (str != '') {
            var obj = {};
            str = app.globalData.SERVER + 'file/pic/' + str + '.png';
            obj.id = index;
            obj.url = str;
            strarytmp.push(obj);
          }
        }

        var showtype = false;
        if (!data.shopservice && data.shopservice.length > 0) {
          showtype = true;
        }
        var sAttitudeScore = (data.serviceAttitudeScore / data.commentnum).toFixed(0);
        var sSpeedScore = (data.serviceSpeedScore / data.commentnum).toFixed(0);
        
        this.setData({
          info: data,
          showView: showtype,
          pics: strarytmp,
          key: sAttitudeScore,
          key2: sSpeedScore,
        });


      } else {

      }


    });

  },


  adddetial: function () {

    wx.navigateTo({
      url: '../../member/wxindex/index',
    })
  },

});